package com.birdboot.controller;

import com.birdboot.annotation.Controller;
import com.birdboot.annotation.RequestMapping;
import com.birdboot.http.HttpServletRequest;
import com.birdboot.http.HttpServletResponse;
import com.birdboot.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Controller
public class ArticleController {
    @RequestMapping("/writeArticle")
    public void write(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理发表文章!!!!!!!!!!!!!!");
        /*
            1.获取请求体数据[如果不确定则去查看form表单]
            2.判断登录状态[不用做]
            3.数据合法性校验[不用做]
            4.操作数据库[INSERT]
         */
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        try (Connection connection = DBUtil.getConnection();){
            String sql = "INSERT INTO article(title,author,content) VALUES(?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, title);
            ps.setString(2, author);
            ps.setString(3, content);
            //执行预编译SQL
            int i = ps.executeUpdate();
            System.out.println("发表文章成功");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}


